package cn.wolfcode.web.controller;

import cn.wolfcode.ssm.domain.City;
import cn.wolfcode.ssm.domain.Department;
import cn.wolfcode.ssm.domain.Province;
import cn.wolfcode.ssm.page.JsonPage;
import cn.wolfcode.ssm.service.IDepartmentService;
import com.fasterxml.jackson.databind.annotation.JsonAppend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
public class JsonController {

    @Autowired
    IDepartmentService departmentService;



    @RequestMapping("/gettime")
    @ResponseBody
    public JsonPage getTime(){
        Date date = new Date();
        JsonPage jsonPage = new JsonPage();
        jsonPage.setSuccess(true);
        jsonPage.setMsg(date.toLocaleString());
        return jsonPage;
    }


    @RequestMapping("/getDepartments")
    @ResponseBody
    public List<Department> getDepartments(){
        return departmentService.selectAll() ;
    }

    @RequestMapping("/checkedUsername")
    @ResponseBody
    // 模拟用户注册时输入账号,检验账号是否被注册过,
    // 注册过是 msg = 该用户名已存在,请更换,success = false   显示该用户名已存在,请更换
    // 没注册过的是 msg = 该用户名可用, success = true 显示该用户名可用
    public JsonPage checkUsername(String username){
        JsonPage jsonPage = new JsonPage();
        if (username.equals("张三")){
            jsonPage.setSuccess(false);
            jsonPage.setMsg("该用户名已存在,请更换");
        }else{
            jsonPage.setSuccess(true);
            jsonPage.setMsg("该用户名可用");
        }
        return  jsonPage;
    }

    // 模拟用户登陆
    @RequestMapping("/loginJson")
    @ResponseBody
    public JsonPage loginJson(String username,String password){
        JsonPage jsonPage = new JsonPage();
        if (username.equals("刘备")&& password.equals("admin"))
        {
            jsonPage.setSuccess(true);
            jsonPage.setMsg("登陆成功");
        }else{
            jsonPage.setSuccess(false);
            jsonPage.setMsg("账号或密码错误请重新输入");
        }
        return jsonPage;
    }

    // 模拟选择省份
    @RequestMapping("/provinceJson")
    @ResponseBody
    public List<Province> getProvince(){
        List<Province> provinces = Province.getAllProvince();
        return provinces;
    }

    // 模拟选择城市
    @RequestMapping("/citiesJson")
    @ResponseBody
    public List<City> getCity(Long pid){
        List<City> cities = City.getCityByProvinceId(pid);
        return cities;
    }

}
